import 'package:flutter/material.dart';

class SubjectList extends StatefulWidget {
  final int total;
  final List list;
  final void Function(dynamic item) onclick;
  final void Function() upRefresh;
  const SubjectList({Key key, this.total, this.list, this.onclick, this.upRefresh})
      : super(key: key);

  @override
  _SubjectListState createState() => _SubjectListState();
}

class _SubjectListState extends State<SubjectList> {

  bool isLoading = true;
  ScrollController _scrollController = new ScrollController();

  @override
  void dispose() {
    super.dispose();
  }

  @override
  void initState() {
    super.initState();
    _scrollController.addListener(() {
      if (_scrollController.position.pixels ==
          _scrollController.position.maxScrollExtent) {
        if (widget.list.length < widget.total) {
          widget.upRefresh();
        }
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Color.fromRGBO(248, 248, 249, 1),
      padding: EdgeInsets.fromLTRB(16, 16, 16, 0),
      child: ListView.builder(
        controller: _scrollController,
        itemCount: widget.list.length + 1,
        itemBuilder: (BuildContext context, int index) {
          if (widget.list.length == 0) {
            return Center(child: Text('暂无数据'));
          } else if (widget.list.length == index) {
            return _buildProgressIndicator();
          } else {
            return createItem(widget.list[index]);
          }
        },
      ),
    );
  }

  Widget _buildProgressIndicator() {
    return new Padding(
      padding: EdgeInsets.only(bottom: 16),
      child: new Center(
        child: new Opacity(
          opacity: isLoading ? 1.0 : 00,
          // child: new CircularProgressIndicator(),
          child: widget.list.length == widget.total
              ? Text('已加载全部')
              : Text('加载中...'),
        ),
      ),
    );
  }

  Widget createItem(item) {
    return GestureDetector(
      onTap: () {
        widget.onclick(item);
      },
      child: Container(
        width: double.infinity,
        height: 124.5,
        margin: EdgeInsets.only(bottom: 12),
        padding: EdgeInsets.all(16),
        decoration: BoxDecoration(
          color: Color.fromRGBO(255, 255, 255, 1),
          border: Border.all(
            width: 0.5,
            color: Color.fromRGBO(229, 229, 229, 1),
          ),
          borderRadius: BorderRadius.circular(3),
        ),
        child: Column(
          children: <Widget>[
            Expanded(
              child: Container(
                padding: EdgeInsets.only(bottom: 16),
                decoration: BoxDecoration(
                  border: Border(
                    bottom: BorderSide(
                      width: 0.5,
                      color: Color.fromRGBO(229, 229, 229, 1),
                    ),
                  ),
                ),
                child: Column(
                  children: <Widget>[
                    Expanded(
                      child: Row(children: <Widget>[
                        Container(
                          alignment: Alignment.centerLeft,
                          child: Text(
                            item.code,
                            style: TextStyle(
                              fontSize: 14,
                              height: 20 / 14,
                            ),
                          ),
                        ),
                        Expanded(
                          child: Container(
                            alignment: Alignment.centerRight,
                            child: Text(
                              item.generateStatus == 'YES' ? '已生成' : '',
                              style: TextStyle(
                                fontSize: 14,
                                height: 20 / 14,
                                color: Color.fromRGBO(89, 131, 252, 1),
                              ),
                            ),
                          ),
                        )
                      ]),
                    ),
                    Row(
                      children: <Widget>[
                        Container(
                          alignment: Alignment.centerLeft,
                          child: Text(
                            item.largeName + " > " + item.subName,
                            style: TextStyle(
                              fontSize: 12,
                              height: 16 / 12,
                              color: Color.fromRGBO(150, 151, 154, 1),
                            ),
                          ),
                        ),
                        Expanded(
                          child: Container(
                            alignment: Alignment.centerRight,
                            child: Text(
                              item.taskCode,
                              style: TextStyle(
                                fontSize: 12,
                                height: 16 / 12,
                                color: Color.fromRGBO(150, 151, 154, 1),
                              ),
                            ),
                          ),
                        )
                      ],
                    ),
                  ],
                ),
              ),
            ),
            Container(
              height: 28,
              padding: EdgeInsets.only(top: 12),
              child: Row(
                children: <Widget>[
                  Container(
                    alignment: Alignment.centerLeft,
                    child: Text(
                      item.customerName,
                      style: TextStyle(
                        fontSize: 12,
                        height: 16 / 12,
                        color: Color.fromRGBO(150, 151, 154, 1),
                      ),
                    ),
                  ),
                  Expanded(
                    child: Container(
                      alignment: Alignment.centerRight,
                      child: Text(
                        item.createTime,
                        style: TextStyle(
                          fontSize: 12,
                          height: 16 / 12,
                          color: Color.fromRGBO(150, 151, 154, 1),
                        ),
                      ),
                    ),
                  )
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}
